home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / program / bgui12.lha / demos / BGUIPlayer / config.c < prev    next >
C/C++ Source or Header  |  1995-05-06  |  10KB  |  281 lines

  1. /*
  2.  *      CONFIG.C
  3.  */
  4.  
  5. #include "BGUIPlayer.h"
  6.  
  7. /*
  8.  *      Parse and evaluate command.
  9.  */
  10. static ULONG ParseConfigComm( struct RDArgs *rda, CONFIGCOMM *parser, UBYTE *argstr )
  11. {
  12.         UBYTE                   *temp = parser->cc_ArgTemplate;
  13.         ULONG                   *args, rc = 0L;
  14.         UWORD                    nargs = 0;
  15.  
  16.         /*
  17.          *      Args expected?
  18.          */
  19.         if ( ! temp ) {
  20.                 ( parser->cc_Func )( NULL );
  21.                 return;
  22.         }
  23.  
  24.         /*
  25.          *      Count the number of arguments we can expect.
  26.          */
  27.         while ( *temp ) {
  28.                 if ( *temp == ',' ) nargs++;
  29.                 temp++;
  30.         }
  31.         nargs++;
  32.  
  33.         /*
  34.          *      Allocate storage for the parsed result.
  35.          */
  36.         if ( args = ( ULONG * )AllocVec( nargs * sizeof( ULONG ), MEMF_PUBLIC | MEMF_CLEAR )) {
  37.                 /*
  38.                  *      Setup RDArgs structure.
  39.                  */
  40.                 rda->RDA_Source.CS_Buffer = argstr;
  41.                 rda->RDA_Source.CS_Length = strlen( argstr );
  42.                 rda->RDA_Source.CS_CurChr = 0;
  43.                 rda->RDA_DAList           = NULL;
  44.                 rda->RDA_Buffer           = NULL;
  45.                 /*
  46.                  *      Parse arguments.
  47.                  */
  48.                 if ( ReadArgs( parser->cc_ArgTemplate, args, rda )) {
  49.                         /*
  50.                          *      Call the evaluation routine.
  51.                          */
  52.                         ( parser->cc_Func )( args );
  53.                         FreeArgs( rda );
  54.                 } else
  55.                         rc = IoErr();
  56.                 FreeVec( args );
  57.         } else
  58.                 rc = ERROR_NO_FREE_STORE;
  59.  
  60.         return( rc );
  61. }
  62.  
  63. /*
  64.  *      Read a configuration file.
  65.  */
  66. Prototype LONG ReadConfigFile( UBYTE *, CONFIGCOMM *, ULONG * );
  67.  
  68. LONG ReadConfigFile( UBYTE *name, CONFIGCOMM *parsers, ULONG *linenum )
  69. {
  70.         struct RDArgs          *rda;
  71.         struct ConfigComm      *reset = parsers;
  72.         BPTR                    file;
  73.         UBYTE                   lbuf[ 512 ];
  74.         ULONG                   rc = 0L;
  75.         UWORD                   i;
  76.  
  77.         /*
  78.          *      Clear line number.
  79.          */
  80.         *linenum = 0;
  81.  
  82.         /*
  83.          *      Allocate a RDArgs structure.
  84.          */
  85.         if ( rda = ( struct RDArgs * )AllocDosObject( DOS_RDARGS, NULL )) {
  86.                 /*
  87.                  *      No prompting.
  88.                  */
  89.                 rda->RDA_Flags |= RDAF_NOPROMPT;
  90.                 /*
  91.                  *      Open the file.
  92.                  */
  93.                 if ( file = Open( name, MODE_OLDFILE )) {
  94.                         /*
  95.                          *      Read the file line-by-line.
  96.                          */
  97.                         while ( FGets( file, lbuf, 512 )) {
  98.                                 /*
  99.                                  *      Start from the beginning.
  100.                                  */
  101.                                 i = 0;
  102.                                 /*
  103.                                  *      Increase line number.
  104.                                  */
  105.                                 *linenum += 1;
  106.                                 /*
  107.                                  *      Skip leading spaces.
  108.                                  */
  109.                                 while ( lbuf[ i ] == ' ' || lbuf[ i ] == '\t' ) i++;
  110.                                 /*
  111.                                  *      Characters left?
  112.                                  */
  113.                                 if ( ! lbuf[ i ] || lbuf[ i ] == '\n' )
  114.                                         goto nextLine;
  115.                                 /*
  116.                                  *      A comment line?
  117.                                  */
  118.                                 if ( lbuf[ i ] != ';' && lbuf[ i ] != '#' ) {
  119.                                         /*
  120.                                          *      Look for the correct command parser.
  121.                                          */
  122.                                         while ( parsers->cc_Name ) {
  123.                                                 /*
  124.                                                  *      Is this the one?
  125.                                                  */
  126.                                                 if ( ! strnicmp( &lbuf[ i ], parsers->cc_Name, strlen( parsers->cc_Name ))) {
  127.                                                         /*
  128.                                                          *      Skip the command name.
  129.                                                          */
  130.                                                         i += strlen( parsers->cc_Name );
  131.                                                         /*
  132.                                                          *      Parse and evaluate args.
  133.                                                          */
  134.                                                         rc = ParseConfigComm( rda, parsers, &lbuf[ i ] );
  135.                                                         /*
  136.                                                          *      Error?
  137.                                                          */
  138.                                                         if ( rc )
  139.                                                                 goto error;
  140.                                                         goto nextLine;
  141.                                                 }
  142.                                                 /*
  143.                                                  *      Next...
  144.                                                  */
  145.                                                 parsers++;
  146.                                         }
  147.                                         /*
  148.                                          *      Command found?
  149.                                          */
  150.                                         if ( ! parsers->cc_Name ) {
  151.                                                 rc = ERROR_BAD_TEMPLATE;
  152.                                                 goto error;
  153.                                         }
  154.                                 }
  155.                                 nextLine:
  156.                                 parsers = reset;
  157.                         }
  158.                         error:
  159.                         /*
  160.                          *      Closeup the file.
  161.                          */
  162.                         Close( file );
  163.                 } else
  164.                         rc = ERROR_OBJECT_NOT_FOUND;
  165.                 /*
  166.                  *      Free the RDArgs structure.
  167.                  */
  168.                 FreeDosObject( DOS_RDARGS, rda );
  169.         } else
  170.                 rc = ERROR_NO_FREE_STORE;
  171.  
  172.         return( rc );
  173. }
  174.  
  175. /*
  176.  *      Global configuratio data.
  177.  */
  178. Prototype UBYTE DeviceName[ 108 ], Popkey[ 128 ], *PubScreen, DiskPath[ 256 ];
  179. Prototype ULONG DevID, Popup;
  180.  
  181. UBYTE DeviceName[ 108 ]   = "scsi.device";
  182. UBYTE Popkey[ 128 ]       = "lshift control c";
  183. UBYTE PubScreenName[ 64 ], *PubScreen;
  184. UBYTE DiskPath[ 256 ];
  185. ULONG DevID, Popup;
  186.  
  187. /*
  188.  *      The configuration commands.
  189.  */
  190. static VOID ParseDevice( ULONG * );
  191. static VOID ParsePopKey( ULONG * );
  192. static VOID ParsePubScreen( ULONG * );
  193. static VOID ParsePopup( ULONG * );
  194. static VOID ParseDiskPath( ULONG * );
  195.  
  196. static CONFIGCOMM ConfigCommands[] = {
  197.         { "DEVICE",     "NAME/K,BOARD/K/N,LUN/K/N,ADDRESS/K/N", ParseDevice     },
  198.         { "POPKEY",     "KEY/A/F",                              ParsePopKey     },
  199.         { "PUBSCREEN",  "SCREEN/K,DEFAULT/S",                   ParsePubScreen  },
  200.         { "POPUP",      "YES/S,NO/S",                           ParsePopup      },
  201.         { "DISKPATH",   "PATH/F",                               ParseDiskPath   }
  202. };
  203.  
  204. /*
  205.  *      Evaluate the DEVICE command.
  206.  */
  207. static VOID ParseDevice( ULONG *args )
  208. {
  209.         /*
  210.          *      Copy device name.
  211.          */
  212.         if ( args[ 0 ] ) strcpy( DeviceName, ( UBYTE * )args[ 0 ] );
  213.         /*
  214.          *      Evaluate Device ID.
  215.          */
  216.         if ( args[ 1 ] ) DevID += *(( LONG * )args[ 1 ] ) * 100;
  217.         if ( args[ 2 ] ) DevID += *(( LONG * )args[ 2 ] ) * 10;
  218.         if ( args[ 3 ] ) DevID += *(( LONG * )args[ 3 ] ); else DevID += 1;
  219. }
  220.  
  221. /*
  222.  *      Evaluate the POPKEY command.
  223.  */
  224. static VOID ParsePopKey( ULONG *args )
  225. {
  226.         strncpy( Popkey, ( UBYTE * )args[ 0 ], 128 );
  227. }
  228.  
  229. /*
  230.  *      Evaluate the PUBSCREEN command.
  231.  */
  232. static VOID ParsePubScreen( ULONG *args )
  233. {
  234.         /*
  235.          *      Name specified?
  236.          */
  237.         if ( args[ 0 ] ) {
  238.                 strncpy( PubScreenName, ( UBYTE * )args[ 0 ], 64 );
  239.                 PubScreen = PubScreenName;
  240.         } else
  241.                 /*
  242.                  *      No. Use default screen.
  243.                  */
  244.                 PubScreen = NULL;
  245. }
  246.  
  247. /*
  248.  *      Evaluate the POPUP command.
  249.  */
  250. static VOID ParsePopup( ULONG *args )
  251. {
  252.         /*
  253.          *      No?
  254.          */
  255.         if ( args[ 1 ] ) Popup = 0;
  256.         else             Popup = 1;
  257. }
  258.  
  259. /*
  260.  *      Evaluate the DISKPATH command.
  261.  */
  262. static VOID ParseDiskPath( ULONG *args )
  263. {
  264.         if ( args[ 0 ] ) strncpy( DiskPath, ( UBYTE * )args[ 0 ], 256 );
  265. }
  266.  
  267. /*
  268.  *      Read the configuration file. First we try
  269.  *      to load it from PROGDIR: and if that fails
  270.  *      we try it in ENVARC:
  271.  */
  272. Prototype VOID LoadConfig( void );
  273.  
  274. VOID LoadConfig( void )
  275. {
  276.         ULONG           line = 0;
  277.  
  278.         if ( ReadConfigFile( "PROGDIR:bgp.prefs", ConfigCommands, &line ) == ERROR_OBJECT_NOT_FOUND )
  279.                 ReadConfigFile( "ENVARC:bgp.prefs", ConfigCommands, &line );
  280. }
  281.